﻿using LottieSharp.WPF;
using LottieSharp.WPF.Transforms;
using SkiaSharp;
using SkiaSharp.Skottie;
using SkiaSharp.Views.Desktop;
using System;
using System.Diagnostics;
using System.IO;

namespace FAQ_UI
{
	public class LottieDrawable : SKDrawable
	{
		internal readonly Stopwatch watch = new Stopwatch();

		internal Animation animation;

		internal int loopCount;

		private byte[] bytes1;

		public LottieDrawable(byte[] bytes)
		{
			this.bytes1 = bytes;
			SetAnimation(new MemoryStream(bytes));
			PlayAnimation();
			Repeat = RepeatMode.Restart;
			RepeatCount = Defaults.RepeatCountInfinite;
		}

		public event EventHandler OnStop;

		public virtual void PlayAnimation()
		{
			watch.Start();
			IsPlaying = true;
		}

		public virtual void StopAnimation()
		{
			loopCount = RepeatCount;
			watch.Reset();
			IsPlaying = false;
			this.OnStop?.Invoke(this, null);
		}

		public AnimationInfo Info { get; set; }

		public int RepeatCount { get; set; }
		public AnimationTransformBase AnimationScale { get; set; }

		public bool AutoPlay { get; set; }

		public bool IsPlaying { get; set; }

		public RepeatMode Repeat { get; set; }

		public void SetAnimation(Stream stream)
		{
			using (SKManagedStream stream2 = new SKManagedStream(stream))
			{
				if (Animation.TryCreate(stream2, out animation))
				{
					animation.Seek(0.0);
					Info = new AnimationInfo(animation.Version, animation.Duration, animation.Fps, animation.InPoint, animation.OutPoint);
					watch.Reset();
					if (AutoPlay || IsPlaying)
					{
						PlayAnimation();
					}
					return;
				}
			}
			Info = new AnimationInfo(string.Empty, TimeSpan.Zero, 0.0, 0.0, 0.0);
			throw new InvalidOperationException("Failed to load animation");
		}

		public override Drawable mutate()
		{
			return new LottieDrawable(bytes1);
		}

		public override double getIntrinsicWidth()
		{
			return 150;
		}

		public override double getIntrinsicHeight()
		{
			return 150;
		}

		protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
		{
			SKCanvas canvas = e.Surface.Canvas;
			canvas.Clear(SKColor.Empty);
			SKImageInfo info = e.Info;
			if (animation == null)
			{
				return;
			}

			animation.SeekFrameTime((float)watch.Elapsed.TotalSeconds);
			if (watch.Elapsed.TotalSeconds > animation.Duration.TotalSeconds && Repeat == RepeatMode.Restart)
			{
				if (RepeatCount == Defaults.RepeatCountInfinite)
				{
					watch.Restart();
				}
				else if (RepeatCount > 0 && loopCount > 0)
				{
					loopCount--;
					watch.Restart();
				}
				else
				{
					StopAnimation();
				}
			}
			if (AnimationScale is CenterTransform)
			{
				canvas.Scale(AnimationScale.ScaleX, AnimationScale.ScaleY, info.Width / 2, info.Height / 2);
			}
			else if (AnimationScale != null)
			{
				canvas.Scale(AnimationScale.ScaleX, AnimationScale.ScaleY, AnimationScale.CenterX, AnimationScale.CenterY);
			}

			animation.Render(canvas, new SKRect(0f, 0f, info.Width, info.Height));
			if (IsPlaying) invalidateSelf();
		}
	}
}